In [1]:
for n in range(10):
print(n)
In [2]:
for fruit in ['apple', 'banana', 'orange']:
print(fruit)
In [3]:
from string import ascii_letters, ascii_lowercase, ascii_uppercase
In [4]:
for letter in ascii_letters:
print(letter, end=' ')
In [5]:
for n, letter in enumerate(ascii_lowercase):
print("{}:{}".format(n, letter), end=' ')
In [6]:
for letter, LETTER in zip(ascii_lowercase, ascii_uppercase):
print('{}{}'.format(letter, LETTER), end=' ')
In [7]:
def fact(n: int) -> int:
if n <= 0:
return 1
return n * fact(n - 1)
In [8]:
for n in range(10):
print("{}: fact:{}".format(n, fact(n)))
In [9]:
def count(word: str, letter: str, start=0) -> int:
if not word:
return 0
return (1 if word[start] == letter else 0) + count(word[start + 1:], letter, 0)
In [10]:
for letter in ascii_lowercase:
print('{}:{}'.format(letter, count(ascii_lowercase, letter)), end=' ')
In [11]:
from random import choice
start_at = choice(range(len(ascii_lowercase)))
for letter in ascii_lowercase:
print('{}:{}'.format(letter, count(ascii_lowercase, letter, start_at)), end=' ')
In [12]:
value = 0
while value < 10:
print(value)
value += 1
In [13]:
value = choice(range(100))
print("Sequence to {}: ".format(value))
while True:
if value == 1:
break
elif value % 2 == 0:
value //= 2
else:
value = value * 3 + 1
print(value, end=' ')
In [14]:
g = (x**2 for x in range(5))
print(g)
In [15]:
print(next(g))
In [16]:
print(next(g))
In [17]:
for val in g:
print(val)
In [18]:
#next(g)
#---------------------------------------------------------------------------
#StopIteration Traceback (most recent call last)
#<ipython-input-6-5f315c5de15b> in <module>()
#----> 1 next(g)
#
#StopIteration:
In [19]:
print(sum(x**2 for x in range(5)))
In [20]:
print(max(x**2 for x in range(5)))
In [21]:
print(min(x**2 for x in range(5)))